package web;

import ejb.CategoryBeanLocal;
import ejb.ProductBeanLocal;
import entities.Category;
import entities.Product;
import java.io.IOException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import web.models.ProductModel;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class AdmProductBean {

    @EJB
    private CategoryBeanLocal categoryBean;
    @EJB
    private ProductBeanLocal productBean;
    private Product selectedProduct;
    private Product newProduct;
    private String categoryName;
    private ProductModel productModel;
    private List<Category> categories;

    public AdmProductBean() {
    }

    @PostConstruct
    public void init() {
        newProduct = new Product();
        productModel = new ProductModel(productBean.findAll());
        categories = categoryBean.findAll();
    }

    public Product getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(Product selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public ProductModel getProductModel() {
        return productModel;
    }

    public void setProductModel(ProductModel productModel) {
        this.productModel = productModel;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public Product getNewProduct() {
        return newProduct;
    }

    public void setNewProduct(Product newProduct) {
        this.newProduct = newProduct;
    }

    public void insertProduct() {
        Category category = categoryBean.findByName(categoryName);
        newProduct.setCategory(category);
        try {
            newProduct = productBean.insert(newProduct);
            ((List<Product>) productModel.getWrappedData()).add(newProduct);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El producto " + newProduct.getName() + " ha sido añadido.", ""));
            newProduct = new Product();
        } catch (EJBException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al añadir el producto.", ""));

        }
    }

    public void updateProduct() {
        System.out.println("UPDATE: " + categoryName);

        Category category = categoryBean.findByName(categoryName);
        selectedProduct.setCategory(category);
        try {
            productBean.update(selectedProduct);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El producto " + selectedProduct.getName() + " ha sido actualizado.", ""));
        } catch (EJBException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al actualizar el producto.", ""));
        }
    }

    public void onRowSelect(SelectEvent event) {
        selectedProduct = (Product) event.getObject();
        categoryName = selectedProduct.getCategory().getName();
        System.out.println(selectedProduct.getCategory().getName());
    }

    public void clearCurrentProduct() {
        newProduct = new Product();
    }

    public void onRowEdit(RowEditEvent event) {
        Product p = (Product) event.getObject();
        p = productBean.update(p);
        if (p != null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "El producto " + p.getName() + " ha sido actualizado", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Ha ocurrido un error al actualizar el producto.", ""));
        }
    }

    public SelectItem[] getCategoriesList() {
        List<Category> cats = categoryBean.findAll();
        SelectItem[] items = new SelectItem[cats.size()];
        for (int i = 0; i < cats.size(); i++) {
            Category c = cats.get(i);
            items[i] = new SelectItem(c, c.getName());
        }
        return items;
    }

    public void goToProduct(Long id) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Tiendalia-war/tiendalia/product.xhtml?product=" + id.toString());
        } catch (IOException ex) {
        }
    }
}
